package C037;

/**
 * ClassName: C02
 * Package: C037
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/1/23 15:45
 * @Version 1.0
 * @Since 1.0
 */
public class C02 {
    public static void main(String[] args) {

    }
    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q) {
            return root;
        }
        if(root.val < p.val && root.val < q.val) {
            return lowestCommonAncestor(root.right, p, q);
        }

        if(root.val > p.val && root.val > q.val) {
            return lowestCommonAncestor(root.left, p, q);
        }

        return root;
    }
}
